#! /usr/bin/python

# To change this template, choose Tools | Templates
# and open the template in the editor.
# todo
#   +tip function
#   +print sudoku
#   +layout mainwidget resize to table

__author__="martin"
__date__ ="$26.07.2010 18:46:22$"

from PyQt4 import QtGui, QtCore
import sudoku
import sys
import scipy

max_rows=sudoku.size
max_columns=sudoku.size

def main():
    app = QtGui.QApplication(sys.argv)
    gui=gui_menue()
    gui.show()

    sys.exit(app.exec_())


class gui_menue(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        vbox=QtGui.QGridLayout(self)


        heading_label=QtGui.QLabel("sudoku solver")
        vbox.addWidget(heading_label, 0, 0, 1, 3)

        load_button=QtGui.QPushButton("load sudoku")
        vbox.addWidget(load_button, 1, 0)
        save_button=QtGui.QPushButton("save sudoku")
        vbox.addWidget(save_button, 2, 0)
        solve_button=QtGui.QPushButton("solve")
        vbox.addWidget(solve_button, 1, 1)
        create_button=QtGui.QPushButton("create")
        vbox.addWidget(create_button, 2, 1)
        clear_button=QtGui.QPushButton("clear")
        vbox.addWidget(clear_button, 1, 2)
        check_button=QtGui.QPushButton("check")
        vbox.addWidget(check_button, 3, 0)
        self.difficulty_box=QtGui.QComboBox()
        self.difficulty_box.insertItem(0, "easy")
        self.difficulty_box.insertItem(1, "medium")
        self.difficulty_box.insertItem(2, "hard")
        self.difficulty_box.insertItem(3, "pro")
        vbox.addWidget(self.difficulty_box, 2, 2)
        tip_button=QtGui.QPushButton("tip")
        vbox.addWidget(tip_button, 3, 1)
        print_button=QtGui.QPushButton("print")
        vbox.addWidget(print_button, 3, 2)
        self.sudoku_table=QtGui.QTableWidget(max_rows, max_columns)
        vbox.addWidget(self.sudoku_table, 4, 0, 1, 3)
        quit_button=QtGui.QPushButton("quit")
        vbox.addWidget(quit_button,  5, 0, 1, 3)
        self.connect(quit_button,  QtCore.SIGNAL('clicked()'), self,
		QtCore.SLOT('close()') )
        self.connect(clear_button,  QtCore.SIGNAL('clicked()'),
		self.clear_sudoku )
        self.connect(save_button,  QtCore.SIGNAL('clicked()'),
		self.save_sudoku )
        self.connect(load_button,  QtCore.SIGNAL('clicked()'),
		self.load_sudoku )
        self.connect(solve_button,  QtCore.SIGNAL('clicked()'),
		self.solve )
        self.connect(create_button,  QtCore.SIGNAL('clicked()'),
		self.create)
        self.connect(check_button,  QtCore.SIGNAL('clicked()'),
		self.check)
        self.connect(tip_button,  QtCore.SIGNAL('clicked()'),
		self.tip)
        self.connect(print_button,  QtCore.SIGNAL('clicked()'),
		self.print_sudoku)
        self.clear_sudoku()
        self.filename=""
        max_w=10.5*self.sudoku_table.columnWidth(0)
        max_h=15.25*self.sudoku_table.rowHeight(0)
        self.resize(max_w, max_h)
        #self.sudoku_table.resize(self.sudoku_table.horizontalHeader().length(), self.sudoku_table.verticalHeader().length())


    def check(self):
        self.update_mat()
        res=sudoku.control_mat(self.mat)
        if res:
            txt="seems ok"
        else:
            txt="sudoku cannot be solved!"
        self.message_box(txt)

    def tip(self):
        self.update_mat()
        res=sudoku.control_mat(self.mat)
        if res:
            #print self.mat
            smat=sudoku.solve_sudoku(self.mat)
            search =1
            x=0
            y=0
            while (y < max_rows) and search:
                #print self.mat[x,y]
                if self.mat[y, x]==0.0:
                    self.mat[y, x]=smat[y, x]
                    search=0
                    #print self.mat[x, y]
                    self.update_table()
                x+=1
                if (x >= max_columns):
                    y+=1
                    x=0
        else:
            txt="sudoku cannot be solved!"
            self.message_box(txt)

    def print_sudoku(self):
        pix=QtGui.QPixmap.grabWidget(self.sudoku_table)
        printer=QtGui.QPrinter()
        #printer.setOutputFormat(0)
        #printer.setOutputFileName("temp.pdf")
        painter=QtGui.QPainter()
        painter.begin(printer)
        painter.drawPixmap(0, 0, pix)
        painter.end()


    def create(self):
        if self.difficulty_box.currentIndex()==0:
            cells=48
        elif self.difficulty_box.currentIndex()==1:
            cells=32
        elif self.difficulty_box.currentIndex()==2:
            cells=24
        else:
            cells=12
        self.mat=sudoku.create_sudoku(cells)
        self.update_table()
        
    def clear_sudoku(self):
        self.mat=scipy.zeros([max_columns, max_rows])
        self.update_table()

    def load_sudoku(self):
        self.filename=  QtGui.QFileDialog.getOpenFileName(self, "open file", "", "*.txt")
        self.mat=sudoku.load_sudoku(self.filename)
        self.update_table()

    def save_sudoku(self):
        self.filename=  QtGui.QFileDialog.getSaveFileName(self, "save file", "", "*.txt")
        self.update_mat()
        sudoku.save_sudoku(self.filename, self.mat)

    def update_table(self):
        for y in range(0, max_rows):
            for x in range(0, max_columns):
                if self.mat[x, y]!=0:
                    i= QtGui.QTableWidgetItem(QtCore.QString.number(self.mat[x, y] ), 0 )
                else:
                    i= QtGui.QTableWidgetItem(QtCore.QString(""))
                self.sudoku_table.setItem(x, y, i )
                i.setTextAlignment(0x4|0x80)
        self.sudoku_table.resizeColumnsToContents()

    def update_mat(self):
        for y in range(0, max_rows):
            for x in range(0, max_columns):
                e=self.sudoku_table.item(x, y)
                n=str(e.text())
                if not n.isdigit():
                    self.mat[x, y]=0
                else:
                    m=int(n)
                    if m <= sudoku.size and m >= 0:
                        self.mat[x, y]=m
                    else:
                        self.mat[x, y]=0

    def solve(self):
        self.update_mat()
        res=sudoku.control_mat(self.mat)
        if res:
            self.mat=sudoku.solve_sudoku(self.mat)
        else:
            self.message_box("sudoku cannot be solved!")
        self.update_table()

    def message_box(self, st):
        msg_box=QtGui.QMessageBox()
        msg_box.setText(st)
        msg_box.exec_()
        


if __name__ == "__main__":
    main()
